package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黑马程序员
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    /**
     * 生产订单
     *
     * @param orders 订单对象
     * @param user   登录的用户
     */
    @Override
    @Transactional
    public void submit(Orders orders, User user) {

        //获取用户的购物车列表数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAllByUid(user.getId());

        //根据地址id获取用户收货地址对象
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());

        //遍历购物车列表数据
        BigDecimal total = new BigDecimal("0");

        //生成订单主键
        Integer oid = UUIDUtils.getUUIDInOrderId();
        //生成订单号
        Integer nid = UUIDUtils.getUUIDInOrderId();
        //遍历购物车记录表数据封装到每个订单明细表数据
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {

            //multiply方法  用于相乘的计算
            //计算每个商品的小计金额=数量*单价
             BigDecimal subTotal = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber()+""));
             //累计求和
             total = total.add(subTotal);

             //快捷键Alt+Enter
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName()); //商品名字
            orderDetail.setOrderId(new Long(oid)); //订单主键
            orderDetail.setDishId(shoppingCart.getDishId()); //菜品id
            orderDetail.setSetmealId(shoppingCart.getSetmealId()); //套餐id
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor()); //口味
            orderDetail.setNumber(shoppingCart.getNumber()); //份数
            orderDetail.setAmount(shoppingCart.getAmount()); //单价
            orderDetail.setImage(shoppingCart.getImage()); //商品图片
            //加入到集合中
            orderDetailList.add(orderDetail);
        }

        //封装订单主表数据
        orders.setId(new Long(oid)); //主键
        orders.setStatus(1); //订单状态：待付款
        orders.setNumber(new String(String.valueOf(nid)));//订单号
        orders.setUserId(user.getId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());

        //计算总金额
        orders.setAmount(total);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        //订单主表写入到数据库
        orderMapper.save(orders);

        //订单明细表集合写入到数据库
        orderDetailMapper.saveBatch(orderDetailList);
    }

    @Override
    public Page<Orders> findByPage(Integer page, Integer pageSize, String number, LocalDateTime beginTime, LocalDateTime endTime) {
        PageHelper.startPage(page,pageSize);
        List<Orders> ordersList = orderMapper.findByPage(number, beginTime, endTime);
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);
        Page<Orders> ordersPage = new Page<>();
        ordersPage.setRecords(ordersPageInfo.getList());
        ordersPage.setTotal(ordersPageInfo.getTotal());
        ordersPage.setPageSize(ordersPageInfo.getSize());
        return ordersPage;
    }

    @Override
    public Page<OrderDto> findByUserPage(Integer page, Integer pageSize,Long userId) {
        PageHelper.startPage(page,pageSize);
        List<OrderDto> ordersUserList = orderMapper.findByUserPage(userId);
        ordersUserList.forEach(item->{
            List<OrderDetail> userIdByDetail = orderDetailMapper.findUserIdByDetail(item.getId());
            item.setOrderDetails(userIdByDetail);
        });
        PageInfo<OrderDto> ordersUserPageInfo = new PageInfo<>(ordersUserList);
        Page<OrderDto> ordersPage = new Page<>();
        ordersPage.setRecords(ordersUserPageInfo.getList());
        ordersPage.setTotal(ordersUserPageInfo.getTotal());
        ordersPage.setPageSize(ordersUserPageInfo.getSize());
        return ordersPage;
    }
}
